package com.shop.trade.api.controller;

import com.shop.common.model.PayCallMdel;
import com.shop.common.model.param.trade.OrderPayInput;
import com.shop.common.tmpl.BaseController;
import com.shop.common.util.DateUtil;
import com.shop.common.util.SessionUtils;
import com.shop.common.util.excption.UserNotFoundException;
import com.shop.trade.api.api.IPayApi;
import com.shop.trade.api.model.StatisModel;
import com.shop.trade.api.service.IPaySvc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description:
 *
 * @author zhoulk
 * @date: 2019年04月15日  17:41
 */
@RestController
public class PayController extends BaseController implements IPayApi {
    @Autowired
    SessionUtils sessionUtils;
    @Autowired
    IPaySvc iPaySvc;

    @Override
    public String qryPayOrders(OrderPayInput input, HttpServletRequest request) throws UserNotFoundException {
        Integer userId = sessionUtils.USER_ID(request);
        input.setType(input.getType() == null || input.getType() == -1 ? null : input.getType());
        input.setBuyerId(userId);
        if (input.getStartTimeStr() != null) {
            input.setStartTime(DateUtil.paresDate(input.getStartTimeStr()));
        }
        if (input.getEndTimeStr() != null) {
            input.setEndTime(DateUtil.paresDate(input.getEndTimeStr()));
        }
        return returnSvc(iPaySvc.qryPayOrders4P(input));
    }

    @Override
    public String qryPayOrderStatis(@RequestBody OrderPayInput input, HttpServletRequest request) throws UserNotFoundException {
        Integer userId = sessionUtils.USER_ID(request);
        input.setBuyerId(userId);
        return returnSvc(iPaySvc.qryStatis(input));
    }

    @Override
    public String qryPayOderStatisList(@RequestBody StatisModel statisModel, HttpServletRequest request) throws UserNotFoundException {
        Integer userId = sessionUtils.USER_ID(request);
        statisModel.setUserId(userId);
        return returnData(iPaySvc.qryStatisList(statisModel));
    }

    @Override
    public String payValidate(Integer orderId, Boolean isBalance) {
        return returnSvc(iPaySvc.payValidate(orderId, isBalance));
    }

    @Override
    public String payResultValidate(@RequestBody PayCallMdel payCallMdel, HttpServletRequest request) throws UserNotFoundException {
        Integer userId = sessionUtils.USER_ID(request);
        return returnSvc(iPaySvc.payResultValidate(payCallMdel, userId));
    }

    @Override
    public String payResultValidate2(@RequestBody PayCallMdel payCallMdel, HttpServletRequest request) throws UserNotFoundException {
        Integer userId = sessionUtils.USER_ID(request);
        return returnSvc(iPaySvc.payResultValidate2(payCallMdel, userId));
    }
}
